package com.dwei;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

import com.dwei.entity.Entity;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerUtil {
//============================================================================
//parameter
//============================================================================
	private String folder;
	private Configuration configuration = new Configuration();
//============================================================================
//constructor
//============================================================================
	/**
	 * 构造函数
	 * @param folder 模板文件所在路径
	 * @param encoding 默认编码
	 * @throws IOException
	 */
	public FreemarkerUtil (String folder, String encoding) {
		setFolder(folder);
		configuration.setDefaultEncoding(encoding);
		configuration.setOutputEncoding(encoding);
		configuration.setObjectWrapper(new DefaultObjectWrapper());
		try {
			configuration.setDirectoryForTemplateLoading(new File(getFolder()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
//============================================================================
//public
//============================================================================
	public void process(MyConfiguration myConfiguration) throws IOException, TemplateException {
		Iterator<Entity> itrtEntity = myConfiguration.getEntities().iterator();
		Entity entity;
		Writer writer;
		String filePath;
		
		String sourcePath = StringUtils.isEmpty(myConfiguration.getSource())?"":myConfiguration.getSource() + File.separator;
		
		filePath = sourcePath + myConfiguration.getPackageEntity().replace(".", File.separator);
		File fileEntity = new File(filePath);
		if (!fileEntity.exists()) fileEntity.mkdirs();
		Template tempEntity = getTemplate(Global.FTL_ENTITY);
		
		filePath = sourcePath + myConfiguration.getPackageDao().replace(".", File.separator);
		File fileDao = new File(filePath);
		if (!fileDao.exists()) fileDao.mkdirs();
		Template tempDao = getTemplate(Global.FTL_DAO);
		
		filePath = sourcePath + myConfiguration.getPackageMapper().replace(".", File.separator);
		File fileMapper = new File(filePath);
		if (!fileMapper.exists()) fileMapper.mkdirs();
		Template tempMapper = getTemplate(Global.FTL_MAPPER);
		
		filePath = sourcePath + myConfiguration.getPackageService().replace(".", File.separator);
		File fileService = new File(filePath);
		if (!fileService.exists()) fileService.mkdirs();
		Template tempService = getTemplate(Global.FTL_SERVICE);
		
		filePath = sourcePath + myConfiguration.getPackageServiceImp().replace(".", File.separator);
		File fileServiceImp = new File(filePath);
		if (!fileServiceImp.exists()) fileServiceImp.mkdirs();
		Template tempServiceImp = getTemplate(Global.FTL_SERVICE_IMP);
		
		filePath = sourcePath + myConfiguration.getPackageInjection().replace(".", File.separator);
		File fileInjection = new File(filePath);
		if (!fileInjection.exists()) fileInjection.mkdirs();
		Template tempInjection = getTemplate(Global.FTL_INJECTION);
		
		while (itrtEntity.hasNext()) {
			entity = itrtEntity.next();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileEntity, entity.getNameEntity() + ".java")), myConfiguration.getEncoding());
			tempEntity.process(myConfiguration.getEntityObjs(entity), writer);
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileDao, entity.getNameDao() + ".java")), myConfiguration.getEncoding());
			tempDao.process(myConfiguration.getDaoObjs(entity), writer);
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileMapper, entity.getNameMapper() + ".xml")), myConfiguration.getEncoding());
			tempMapper.process(myConfiguration.getMapObjs(entity), writer);
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileService, entity.getNameService() + ".java")), myConfiguration.getEncoding());
			tempService.process(myConfiguration.getServiceObjs(entity), writer);
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileServiceImp, entity.getNameServiceImp() + ".java")), myConfiguration.getEncoding());
			tempServiceImp.process(myConfiguration.getServiceImpObjs(entity), writer);
			writer.close();
			
			writer = new OutputStreamWriter(new FileOutputStream(new File(fileInjection, entity.getNameInjection() + ".xml")), myConfiguration.getEncoding());
			tempInjection.process(myConfiguration.getInjectionObjs(entity), writer);
			writer.close();
		}
	}

	public Template getTemplate(String name) throws IOException {
		return configuration.getTemplate(name, "UTF-8");//读取模板时的编码
	}
//============================================================================
//getter/setter
//============================================================================
	public String getFolder() {
		return folder;
	}
	public void setFolder(String folder) {
		this.folder = folder;
	}
	public Configuration getConfiguration() {
		return configuration;
	}
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
}
